/*
Project: 		The Impact of Short-Term Employment for Low-Income Youth: Experimental 	
				Evidence from the Philippines
Authors: 		Emily A. Beam and Stella Quimbo

***********************************
Path: 			code/02_analysis
File name: 		table_a15.do 
***********************************

Purpose: 		Generates Appendix Table A15, which is a version of Table 3 with additional heterogeneity 

Inputs: 		spes_data.dta
Outputs: 		Table A15 = table3_ivspes_panel_finc.tex
					

Log: 			results/03_output/log_table_a15.log		

Last updated: 	18 November 2021
*/



****************** Load regular data

cap log close
log using "$output/log_table_a15.log",replace

estimates clear
use "$usedata_analysis/spes_data",clear 

	keep if endline == 1



** Loop over list of interactions
local intlist "_bb_wa _eeo_fi _highover"



local n: word count $cov1
	assert `n' == 18 
** Label variables 



local l_eeo_fi "Above-median SES"
local l_bb_wa "Any Experience"
local l_highover "Above-median over-subscription"



label var _eeo_fi "`l_eeo_fi'"
label var _bb_wa "`l_bb_workany'" 

gen _f_highover = 0

* Generate interaction terms 
foreach interact in  `intlist' {
	di in red "`l`linteract''"
* IV terms
foreach mes in tr spes_2016{
	gen `mes'X`interact' = `mes'*`interact'
	label var `mes'X`interact' "SPES X `l`interact''"
	
	gen `mes'X_f`interact' = `mes'*_f`interact'


	}
	
* ITT
gen `interact'xtreatment = `interact'*treatment
	label var `interact'xtreatment "SPES X `l`interact''"

gen _f`interact'xtreatment = _f`interact'*treatment

* Generate inteaction terms for each covariate 

ds $cov1 
foreach cov in `r(varlist)'{ 
 gen `interact'X`cov' = `cov'*`interact'
 gen _f`interact'X`cov' = `cov'*_f`interact'
} 

}




* Set locals 
local depvar1 	_eeo_worknow 		_eeo_jobsearch 			_eeo_wage_mn 		_eeo_workhours  			
local labels1 "	"Currently working (formal)" 	"Currently looking" 		"Current monthly earnings" 			"Work hours weekly now" "
					



										
loc w = 1				
local name1 "employment" 


forval j = 1/1{		
					

local r replace



** Uninteracted version ** 
	foreach var in `depvar`j'' {
	
		
			
foreach mes in  spes_2016{

		xi:  ivregress 2sls  `var' (`mes' = treatment ) $cov1  i.scel if treatment!=. ,  robust
		estimates store iv`mes'`var'
		summ `var' if treatment==0 
		estadd scalar dmean = `r(mean)'


}
	
	}
	foreach var in `depvar`j'' {


foreach interact in `intlist'  {


		
foreach mes in  spes_2016{

	
		xi:  ivregress 2sls  `var' (`mes' `mes'X`interact' = treatment `interact'xtreatment) $cov1 `interact'X* _f`interact' _f`interact'X* i.scel if treatment!=. ,  robust
		
		estimates store iv`mes'`w'`interact'
		di in red "estimates stored as iv`mes'`w'`interact'"
		summ `var' if treatment==0 
		estadd scalar dmean = `r(mean)'
		
		test `mes' + `mes'X`interact' == 0 
		local pval = `r(p)'
		estadd scalar pvalint = `r(p)'
}	// nature of IV loop
		

	
	}		// interaction term
	
	local w = `w' + 1
}		// individual dependent variables within each set
	
}		// dependent variable sets (tables)	

		
//* Interaction, college only 
loc w = 1		
forval j = 1/1{
		foreach var in `depvar`j'' {


*foreach interact in  _bb_female _NT_edu _bb_edulvl{
foreach interact in _bb_wa  {


		
foreach mes in  spes_2016{

	
	qui	xi:  ivregress 2sls  `var' (`mes' `mes'X`interact' = treatment `interact'xtreatment) $cov1 `interact'X* _f`interact' _f`interact'X* i.scel if treatment!=. & _NT_edu == 1,  robust
		
		estimates store iv`mes'`w'`interact'_col
		di in red "estimates stored as iv`mes'`w'`interact'"
		summ `var' if treatment==0 
		estadd scalar dmean = `r(mean)'
		
		test `mes' + `mes'X`interact' == 0 
		local pval = `r(p)'
		estadd scalar pvalint = `r(p)'
}	// nature of IV loop
		

	
	}		// interaction term
	
	local w = `w' + 1
}		// individual dependent variables within each set
	
		// dependent variable sets (tables)	



}




********************************************
********************************************


** Table numbers 

loc tned = 6	// education and employment
loc tnemp = 3	//  employment 
loc tneduc = 5	// education 

*** Footnote text 


local fn`tnemp' = "\multicolumn{5}{p{1.15\textwidth}}{\footnotesize{Notes: All endline respondents included. All specifications include controls listed in Table \ref{tbal} along with stratification-cell fixed effects.  Panels B through E add controls multiplied by the binary interaction term along with uninteracted stratification-cell fixed effects.   \textit{*** p$<$0.01, ** p$<$0.05, * p$<$0.10} }}\\"


********************************************
********************************************
cd "$tables_analysis"
#delimit ; 
********** Table 3 - employment  *************;
{;
	
	// Panel A: Aggregate treatment effects;

	
foreach mes in  spes_2016{;
	
	esttab iv`mes'_eeo_worknow 		iv`mes'_eeo_jobsearch 			iv`mes'_eeo_wage_mn 		iv`mes'_eeo_workhours  	
	using table`tnemp'_iv_`mes'.tex, replace star(* 0.10 ** 0.05 *** 0.01) 
	cells("b(fmt(3) star)" "se(par([ ]))") 
	keep(`mes') label  noobs
	varwidth(16) modelwidth(13) style(tex) 
	title(Impact of SPES on employment\label{temployment}) 
	varlabels(_cons Constant) 
	mlabels("Currently working (formal)" "Currently looking" 
		"Current monthly earnings"  	"Work hours weekly now" , 
			span prefix(\multicolumn{@span}{x{0.15\textwidth}}{) suffix(})) 
	collabels(none)
	prehead( "\begin{tabular}{lcccc}" "\toprule") 
	posthead(\hline) 
	prefoot() 
	postfoot("\bottomrule");
};




	//* Panel B: Any work experience (all); 
	
	
foreach mes in  spes_2016{;

	esttab iv`mes'1_bb_wa		iv`mes'2_bb_wa 			iv`mes'3_bb_wa		iv`mes'4_bb_wa  			  
	using table`tnemp'_iv_`mes'_bb_wa.tex, replace star(* 0.10 ** 0.05 *** 0.01) 
	cells("b(fmt(3) star)" "se(par([ ]))") 
	stats(pvalint   , fmt( %9.3f   )  
	labels( "p-value, SPES + SPES X Any Experience" )) 
	keep(`mes' `mes'X_bb_wa) label  varwidth(16) modelwidth(13) style(tex) 
	title(Impact of SPES on employment\label{temployment}) 
	varlabels(_cons Constant) 
	mlabels("Currently working (formal)" 	"Currently looking" 	
			"Current monthly earnings"  			"Work hours weekly now" ,
			span prefix(\multicolumn{@span}{x{0.15\textwidth}}{) suffix(})) 
	collabels(none)
	prehead("\begin{table}\caption{@title}" "\begin{center}"  "\begin{tabular}{lcccc}" "\toprule") 
		posthead(\hline) 
	prefoot() 
	postfoot("\bottomrule"  );
};


	//* Panel C: Any work experience, college only; 

	
foreach mes in  spes_2016{;

	esttab iv`mes'1_bb_wa_col		iv`mes'2_bb_wa_col 			iv`mes'3_bb_wa_col		iv`mes'4_bb_wa_col  			  
	using table`tnemp'_iv_`mes'_bb_wa_col.tex, replace star(* 0.10 ** 0.05 *** 0.01) 
	cells("b(fmt(3) star)" "se(par([ ]))") 
	stats(pvalint  , fmt( %9.3f  )  
	labels( "p-value, SPES + SPES X Any Experience"  )) 
	keep(`mes' `mes'X_bb_wa) label  varwidth(16) modelwidth(13) style(tex) 
	title(Impact of SPES on employment\label{temployment}) 
	varlabels(_cons Constant) 
	mlabels("Currently working (formal)" 	"Currently looking" 	
		"Current monthly earnings"   			"Work hours weekly now" ,
			span prefix(\multicolumn{@span}{x{0.15\textwidth}}{) suffix(})) 
	collabels(none)
	prehead("\begin{table}\caption{@title}" "\begin{center}"  "\begin{tabular}{lcccc}" "\toprule") 
	prefoot() 
	postfoot("\bottomrule"  );
};



	//* Panel D: High SES; 
	
	
foreach mes in  spes_2016{;

	esttab iv`mes'1_eeo_fi 		iv`mes'2_eeo_fi		iv`mes'3_eeo_fi		iv`mes'4_eeo_fi  			  
	using table`tnemp'_iv_`mes'_eeo_fi.tex, replace star(* 0.10 ** 0.05 *** 0.01) 
	cells("b(fmt(3) star)" "se(par([ ]))") 
	stats( pvalint   , fmt( %9.3f  )  
	labels(  "p-value, SPES + SPES X High SES"  )) 
	keep(`mes' `mes'X_eeo_fi) label  
	varwidth(16) modelwidth(13) style(tex) 
	title(Impact of SPES on employment\label{temployment}) 
	varlabels(_cons Constant) 
	mlabels("Currently working (formal)" 	"Currently looking" 	
		"Current monthly earnings"  	"Work hours weekly now" , 
			span prefix(\multicolumn{@span}{x{0.15\textwidth}}{) suffix(})) 
	collabels(none)
	prehead("\begin{table}\caption{@title}" "\begin{center}"  "\begin{tabular}{lcccc}" "\toprule") 
	posthead(\hline) 	prefoot() 
	postfoot("\bottomrule"  );

};

	//* Panel E High oversub;
	
	
foreach mes in  spes_2016{;

	esttab iv`mes'1_highover 		iv`mes'2_highover	iv`mes'3_highover	iv`mes'4_highover 			  
	using table`tnemp'_iv_`mes'_highover.tex, replace star(* 0.10 ** 0.05 *** 0.01) 
	cells("b(fmt(3) star)" "se(par([ ]))") 
	stats(pvalint N dmean , fmt(%9.3f  %9.0f  %9.3f )  
	labels("p-value, SPES + SPES X High Oversub." "Observations" "Mean, control group" )) 
	keep(`mes' `mes'X_highover) label  
	varwidth(16) modelwidth(13) style(tex) 
	title(Impact of SPES on employment\label{temployment}) 
	varlabels(_cons Constant) 
	mlabels("Currently working (formal)" 	"Currently looking" 	
			"Current monthly earnings"  	"Work hours weekly now" , 
			span prefix(\multicolumn{@span}{x{0.15\textwidth}}{) suffix(})) 
	collabels(none)
	prehead("\begin{table}\caption{@title}" "\begin{center}"  "\begin{tabular}{lcccc}" "\toprule") 
	posthead(\hline) prefoot() postfoot("\bottomrule" "`fn`tnemp''" "\end{tabular}" );

};



// Combine panels ;

#delimit;
include "$dofiles/PanelCombine.do";



// Table 3  - IV - self report ;
panelcombine, use(table`tnemp'_iv_spes_2016.tex  table`tnemp'_iv_spes_2016_bb_wa.tex table`tnemp'_iv_spes_2016_bb_wa_col.tex table`tnemp'_iv_spes_2016_eeo_fi.tex table`tnemp'_iv_spes_2016_highover.tex)  
columncount(5) paneltitles("Aggregate treatment effects" "Interacted by work experience" "Interacted by work experience (college only)" "Interacted by socio-economic status" "Interacted by oversubscription rate")
save(table_a15.tex) cleanup;


};




exit;
